| Conditions | 4 |
| Total Lines | 52 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* |
||
| 77 | export function resample(samples, oldSampleRate, sampleRate, options=null) { |
||
| 78 | options = options || {}; |
||
| 79 | // Make the new sample container |
||
| 80 | /** @type {number} */ |
||
| 81 | let rate = ((sampleRate - oldSampleRate) / oldSampleRate) + 1; |
||
| 82 | /** @type {!Float64Array} */ |
||
| 83 | let newSamples = new Float64Array(samples.length * (rate)); |
||
| 84 | // Create the interpolator |
||
| 85 | options.method = options.method || 'cubic'; |
||
| 86 | /** @type {!Object} */ |
||
| 87 | let interpolator = new Interpolator( |
||
| 88 | samples.length, |
||
| 89 | newSamples.length, |
||
| 90 | { |
||
| 91 | method: options.method, |
||
| 92 | tension: options.tension || 0, |
||
| 93 | sincFilterSize: options.sincFilterSize || 6, |
||
| 94 | sincWindow: options.sincWindow || undefined, |
||
| 95 | clip: options.clip || 'mirror' |
||
| 96 | }); |
||
| 97 | // Resample + LPF |
||
| 98 | if (options.LPF === undefined) { |
||
| 99 | options.LPF = DEFAULT_LPF_USE[options.method]; |
||
| 100 | } |
||
| 101 | if (options.LPF) { |
||
| 102 | options.LPFType = options.LPFType || 'IIR'; |
||
| 103 | const LPF = DEFAULT_LPF[options.LPFType]; |
||
| 104 | // Upsampling |
||
| 105 | if (sampleRate > oldSampleRate) { |
||
| 106 | /** @type {!Object} */ |
||
| 107 | let filter = new LPF( |
||
| 108 | options.LPForder || DEFAULT_LPF_ORDER[options.LPFType], |
||
| 109 | sampleRate, |
||
| 110 | (oldSampleRate / 2)); |
||
| 111 | upsample_( |
||
| 112 | samples, newSamples, interpolator, filter); |
||
| 113 | // Downsampling |
||
| 114 | } else { |
||
| 115 | /** @type {!Object} */ |
||
| 116 | let filter = new LPF( |
||
| 117 | options.LPForder || DEFAULT_LPF_ORDER[options.LPFType], |
||
| 118 | oldSampleRate, |
||
| 119 | sampleRate / 2); |
||
| 120 | downsample_( |
||
| 121 | samples, newSamples, interpolator, filter); |
||
| 122 | } |
||
| 123 | // Resample, no LPF |
||
| 124 | } else { |
||
| 125 | resample_(samples, newSamples, interpolator); |
||
| 126 | } |
||
| 127 | return newSamples; |
||
| 128 | } |
||
| 129 | |||
| 185 |